package com.lanwf.admin.sys;

import com.lanwf.common.model.SysOperate;
import com.lanwf.common.model.SysPermission;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: Lanwf
 * @Date: 2022/2/24 19:33
 * @Description:
 */
public class PermissionService {

    /**
     * accountId = 1 为管理员
     * @param accountId
     * @param menuId
     * @return
     */
    public Map<String,SysOperate> getPermission(Integer accountId, Integer menuId){


        if(accountId != null && accountId != 1) {

            SysPermission sysPermissions = SysPermission.dao.findFirst("select * from sys_permission where account_id = ? and menu_id = ? ", accountId, menuId);

            if (sysPermissions != null && StringUtils.isNotBlank(sysPermissions.getOperates())) {

                HashMap<String, SysOperate> map = new HashMap<>();
                List<SysOperate> sysOperates = SysOperate.dao.find("select * from sys_operate where id in ( " + sysPermissions.getOperates() + ") ");

                for (SysOperate s : sysOperates) {
                    map.put(s.getCode(), s);
                }

                return map;
            }
        }else{
            //管理员 获取所有权限
            List<SysOperate> sysOperates = SysOperate.dao.find("select * from sys_operate ");
            HashMap<String, SysOperate> map = new HashMap<>();

            for (SysOperate s : sysOperates) {
                map.put(s.getCode(), s);
            }
            return map;
        }




        return null;
    }
}
